﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class EditSize : System.Web.UI.UserControl
    {
        public int idproductcolor;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                idproductcolor = Convert.ToInt32(Request.QueryString["idproductcolor"]);
                rpt_productsize.DataSource = DAL.DCollector.DBCollector.Instance.SelectSQL("Select tbl_ProductColor.Product_ID,tbl_Product.Product_Name,tbl_Color.Color_Name,tbl_Size.Size_Name,tbl_ProductSize.ProductSize_ID from tbl_Product,tbl_ProductColor,tbl_Color,tbl_Size,tbl_ProductSize Where tbl_ProductColor.Product_ID = tbl_Product.Product_ID AND tbl_ProductSize.ProductColor_ID = tbl_ProductColor.ProductColor_ID AND tbl_ProductColor.Color_ID = tbl_Color.Color_ID AND tbl_ProductSize.Size_ID = tbl_Size.Size_ID AND tbl_ProductSize.ProductColor_ID = " + idproductcolor + "");
                rpt_productsize.DataBind();

                DataTable dt = DAL.DCollector.DBCollector.Instance.SelectSQL("Select Size_ID,Size_Name from tbl_Size Where Size_ID NOT IN(select Size_ID from tbl_ProductSize Where tbl_ProductSize.ProductColor_ID = " + idproductcolor + ")");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem lt = new ListItem(dt.Rows[i]["Size_Name"].ToString(), dt.Rows[i]["Size_ID"].ToString());
                    cblSize.Items.Add(lt);
                }
            }
        }

        protected void btnUpdateSize_Click(object sender, EventArgs e)
        {
            idproductcolor = Convert.ToInt32(Request.QueryString["idproductcolor"]);
            
            for (int i = 0; i < cblSize.Items.Count; i++)
            {
                int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_ProductSize", "ProductSize_ID"));
                if (cblSize.Items[i].Selected == true)
                {
                    if (nextid == 0)
                    {
                        string value1 = "1," + idproductcolor + "," + cblSize.Items[i].Value + "";
                        List<string> values1 = new List<string>();
                        values1.Add(value1);
                        DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_ProductSize", "insertbyrows", "*", values1, false);
                        values1.Clear();
                        
                    }
                    else
                    {
                        int idproductsize = nextid + 1;
                        string value2 = "" + idproductsize + "," + idproductcolor + "," + cblSize.Items[i].Value + "";
                        List<string> values2 = new List<string>();
                        values2.Add(value2);
                        DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_ProductSize", "insertbyrows", "*", values2, false);
                        values2.Clear();
                        
                    }
                    
                }

            }
            Response.Redirect("/Admin/Index.aspx?panel=1&sub=10&idproductcolor=" + idproductcolor);
        }

        protected void rpt_productsize_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            idproductcolor = Convert.ToInt32(Request.QueryString["idproductcolor"]);
            if (e.CommandName == "delete")
            {
                // Xoa
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows("tbl_ProductSize", "ProductSize_ID", id.ToString());
                Response.Redirect("/Admin/Index.aspx?panel=1&sub=10&idproductcolor=" + idproductcolor);
            }
        }
    }
}